//package com.hxs.hxsserver.socketServer;
//import com.hxs.hxsserver.domain.dto.ResponseCode;
//import com.hxs.hxsserver.domain.entity.device.Device;
//import com.hxs.hxsserver.exception.ApiException;
//import com.hxs.hxsserver.service.SSEService;
//import com.hxs.hxsserver.utils.ByteUtil;
//import com.hxs.hxsserver.utils.CRCUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.PreDestroy;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.net.ServerSocket;
//import java.net.Socket;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//
//@Slf4j
//@Component
//public class SocketServer_back {
//    private static final int PORT = 8083; // 监听的端口
//    private ServerSocket serverSocket;
//    private ExecutorService executorService;
//    // 存储当前连接的客户端
//    private Map<String, ClientHandler> clientHandlers = new ConcurrentHashMap<>();
//    @Autowired
//    private SSEService sseService;
//
//    @PostConstruct
//    public void start() {
//        executorService = Executors.newFixedThreadPool(10); // 创建线程池
//        try {
//            serverSocket = new ServerSocket(PORT);
//            log.info("Socket服务器启动，监听端口 " + PORT);
//
//            // 处理客户端连接
//            new Thread(() -> {
//                try {
//                    while (true) {
//                        Socket clientSocket = serverSocket.accept(); // 等待客户端连接
//                        log.info("客户端连接: " + clientSocket.getInetAddress());
//                        ClientHandler clientHandler = new ClientHandler(clientSocket, sseService,clientHandlers);
////                        clientHandlers.put(clientSocket.getInetAddress().getHostAddress(), clientHandler); // 存储客户端
//
//                        executorService.submit(clientHandler);// 使用线程池处理客户端
//                    }
//                } catch (IOException e) {
//                    log.error("服务器错误: " + e.getMessage());
//
//                }
//            }).start();
//        } catch (IOException e) {
//            log.error("服务器启动失败: " + e.getMessage());
//        }
//    }
//
//    @PreDestroy
//    public void stop() {
//        try {
//            if (serverSocket != null && !serverSocket.isClosed()) {
//                serverSocket.close(); // 关闭服务器
//                executorService.shutdown(); // 关闭线程池
//                clientHandlers.clear();
//                log.info("Socket服务器已关闭");
//            }
//        } catch (IOException e) {
//            log.error("关闭服务器时出错: " + e.getMessage());
//        }
//    }
//    public String sendMessageToClient(String deviceHardwareAddr, String message) {
//        ClientHandler handler = clientHandlers.get(deviceHardwareAddr);
//        // 发送消息
//        if (handler != null) {
//            handler.sendMessage(message);
//            return "成功";
//        } else {
//            throw new ApiException(ResponseCode.DEVICE_STATUS_ERROR.getCode(), ResponseCode.DEVICE_STATUS_ERROR.getDesc());
//        }
//    }
//}
